package com.gr.experimental.model.impl.distr.operations;

import com.gr.experimental.model.impl.distr.Context;
import com.gr.experimental.model.impl.distr.operators.UnaryOperator;

public class UnaryOperation implements Expression {

	Expression expression;
	UnaryOperator operator;
	
	public UnaryOperation(Expression expression,
			UnaryOperator operator) {
		this.expression = expression;
		this.operator = operator;

		if (!operator.getArgumentType().
				isAssignableFrom(expression.getValueType())) {
			throw new IllegalArgumentException("LeftExpression should have type " 
					+ operator.getArgumentType() +  " but has " 
					+ expression.getValueType());
		}
	}

	@Override
	public Object getValue(Context context) throws CalculationException {
		Object value = expression.getValue(context);
		return operator.op(value);
	}

	@Override
	public Class<?> getValueType() {
		return operator.getReturnType();
	}

	public UnaryOperator getOperator() {
		return operator;
	}


}
